#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
#define DEBUG

inline int read()
{
    int c=getchar(), f=1, x=0;
    if(c=='\n') c=getchar();
    if(c=='-') f*=-1, c=getchar();
    while(c<'0'&&'9'<c) c=getchar();
    while('0'<=c&&c<='9') 
        x=(x<<3)+(x<<1)+c-'0', c=getchar();
    return f*x;
}
inline void write(long long x)
{
    if(x>=10) write(x/10);
    putchar(x%10+'0');
}
#define DEBUG
const int N=3e5+10, INF=0x3f3f3f3f;
#define alpha(u) (u?1:val)
const double val=sqrt(0.5);
const double pi=acos(-1);

int n, m, T;
int Q[8][8], M[8][8]; //量化矩阵 填充矩阵
double M1[8][8]; //解码矩阵
bool dir=false; //填充方向 0左下右上 1右上左下

void printarr(int arr[8][8])
{
    for(int i=0; i<8; i++)
    {   
        for(int j=0; j<8; j++)
            printf("%d ", arr[i][j]);
        puts("");
    }
}

void printarr(double arr[8][8])
{
    for(int i=0; i<8; i++)
    {   
        for(int j=0; j<8; j++)
            printf("%.0lf ", arr[i][j]);
        puts("");
    }
}

void init()
{
    for(int i=0; i<8; i++)
        for(int j=0; j<8; j++)
            Q[i][j]=read();

    n=read(), T=read();

    //填充
    int x=0, y=0;
    for (int i=1; i<=n; i++) {
        M[x][y]=read(); //按照填充顺序读取
        if (!dir && (!x || y==7)) dir=true, y==7 ? x++ : y++;
        else if (!dir) x--, y++;
        else if (!y || x==7) dir=false, x==7 ? y++ : x++;
        else x++, y--;
    }
}

void solve() {
    init();

    if (T==0) return printarr(M);
    for (int i=0; i<8; i++)
        for (int j=0; j<8; j++)
            M[i][j] *= Q[i][j]; //量化矩阵
    if (T==1) return printarr(M);
    //最终解码
    for (int i=0; i<8; i++)
        for (int j=0; j<8; j++) {
            for (int u=0; u<8; u++)
                for (int v=0; v<8; v++)
                    M1[i][j] += alpha(u)*alpha(v)*M[u][v]*cos(pi/8*(i+0.5)*u)*cos(pi/8*(j+0.5)*v);
            M1[i][j] /= 4;
            M1[i][j] += 128;
            M1[i][j]=max(M1[i][j], 0.0);
            M1[i][j]=min(M1[i][j], 255.0);
        }
    printarr(M1);
}

signed main()
{
    #ifdef DEBUG
        freopen("../in.txt", "r", stdin);
        freopen("../out.txt", "w", stdout);
    #endif

    int T=1; //cin >> T; 
    while(T--) 
    {
        solve();
    }
    return 0;
}